<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="小程序openid" prop="enduserOpenId">
        <el-input
          v-model="queryParams.enduserOpenId"
          placeholder="请输入openid"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名字" prop="enduserName">
        <el-input
          v-model="queryParams.enduserName"
          placeholder="请输入用户名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="enduserPhone">
        <el-input
          v-model="queryParams.enduserPhone"
          placeholder="请输入电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址" prop="enduserAddress">
        <el-input
          v-model="queryParams.enduserAddress"
          placeholder="请输入终端用户地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="绑定状态" prop="bindState">
        <el-select v-model="queryParams.bindState" placeholder="请选择绑定状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="冻结状态" prop="freezeState">
        <el-select v-model="queryParams.freezeState" placeholder="请选择冻结状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="提现类型" prop="cashoutType">
        <el-select v-model="queryParams.cashoutType" placeholder="请选择提现类型" clearable>
          <el-option
            v-for="dict in dict.type.xw_cashout_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['washer:enduser:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['washer:enduser:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['washer:enduser:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['washer:enduser:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="enduserList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="终端经营用户id" align="center" prop="enduserId" />
      <el-table-column label="小程序openid" align="center" prop="enduserOpenId" />
      <el-table-column label="终端用户名字" align="center" prop="enduserName" />
      <el-table-column label="终端用户电话" align="center" prop="enduserPhone" />
      <el-table-column label="终端用户地址" align="center" prop="enduserAddress" />
      <el-table-column label="提现类型" align="center" prop="cashoutType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.xw_cashout_type" :value="scope.row.cashoutType"/>
        </template>
      </el-table-column>
      <el-table-column label="商户号" align="center" prop="enduserMerchantCode" />
      <el-table-column label="商户名称" align="center" prop="enduserMerchantName" />
      <el-table-column label="冻结状态" align="center" prop="freezeState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.freezeState"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['washer:enduser:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['washer:enduser:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改终端经营用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :inline="true"  label-width="180px">
        <el-form-item label="小程序openid" prop="enduserOpenId">
          <el-input v-model="form.enduserOpenId" placeholder="请输入小程序openid" />
        </el-form-item>
        <el-form-item label="终端用户名字" prop="enduserName">
          <el-input v-model="form.enduserName" placeholder="请输入终端用户名字" />
        </el-form-item>
        <el-form-item label="终端用户电话" prop="enduserPhone">
          <el-input v-model="form.enduserPhone" placeholder="请输入终端用户电话" />
        </el-form-item>
        <el-form-item label="终端用户地址" prop="enduserAddress">
          <el-input v-model="form.enduserAddress" placeholder="请输入终端用户地址" />
        </el-form-item>
        <el-form-item label="提现类型" prop="cashoutType">
          <el-select v-model="form.cashoutType" placeholder="请选择提现类型">
            <el-option
              v-for="dict in dict.type.xw_cashout_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商户号" prop="enduserMerchantCode">
          <el-input v-model="form.enduserMerchantCode" placeholder="请输入商户号" />
        </el-form-item>
        <el-form-item label="商户名称" prop="enduserMerchantName">
          <el-input v-model="form.enduserMerchantName" placeholder="请输入商户名称" />
        </el-form-item>
        <el-form-item label="冻结状态" prop="freezeState">
          <el-select v-model="form.freezeState" placeholder="请选择冻结状态">
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="业务1" prop="enduserBusiness01">
          <el-select v-model="form.enduserBusiness01" placeholder="请选择终端" clearable>
            <el-option
              v-for="dict in newAllEnduserList"
              :key="dict.enduserOpenId"
              :label="dict.enduserName"
              :value="dict.enduserOpenId"
              :disabled="dict.disabled"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="业务1出售提成比例（%）" prop="businessPercent01">
          <el-input v-model="form.businessPercent01" placeholder="请输入提成比例" />
        </el-form-item>
        <el-form-item label="业务1出租提成比例（%）" prop="businessRentPercent01">
          <el-input v-model="form.businessRentPercent01" placeholder="请输入提成比例" />
        </el-form-item>
        <el-form-item label="业务2" prop="enduserBusiness02">
          <el-select v-model="form.enduserBusiness02" placeholder="请选择提现类型" clearable>
            <el-option
              v-for="dict in newAllEnduserList"
              :key="dict.enduserOpenId"
              :label="dict.enduserName"
              :value="dict.enduserOpenId"
              :disabled="dict.disabled"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="业务2出售提成比例（%）" prop="businessPercent02">
          <el-input v-model="form.businessPercent02" placeholder="请输入提成比例" />
        </el-form-item>
        <el-form-item label="业务2出租提成比例（%）" prop="businessPercent02">
          <el-input v-model="form.businessRentPercent02" placeholder="请输入提成比例" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEnduser, getEnduser, delEnduser, addEnduser, updateEnduser } from "@/api/washer/enduser";

export default {
  name: "Enduser",
  dicts: ['sys_yes_no','xw_cashout_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 终端经营用户表格数据
      enduserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      allEnduserList:[],
      newAllEnduserList:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        enduserOpenId: null,
        enduserName: null,
        enduserPhone: null,
        enduserAddress: null,
        bindState: null,
        freezeState: null,
        activationCode: null,
        cashoutType:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },

    };
  },
  created() {
    this.getList();
    this.getAllEnduser();
  },
  methods: {
    getAllEnduser(){
      var queryParams={
        pageNum: 1,
        pageSize: 10000000,
      }
      listEnduser(queryParams).then(response => {
        this.allEnduserList = response.rows;
        console.log(this.allEnduserList);
      });
    },
    /** 查询终端经营用户列表 */
    getList() {
      this.loading = true;
      listEnduser(this.queryParams).then(response => {
        this.enduserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        enduserId: null,
        enduserOpenId: null,
        enduserName: null,
        enduserPhone: null,
        enduserAddress: null,
        bindState: null,
        freezeState: null,
        activationCode: null,
        createBy: null,
        createdTime: null,
        updateBy: null,
        updatedTime: null,
        enduserMerchantCode:null,
        enduserMerchantName:null,
        cashoutType:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.enduserId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加终端经营用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const enduserId = row.enduserId || this.ids
      getEnduser(enduserId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改终端经营用户";
        var enduserOpenId=this.form.enduserOpenId;
        var newAllEnduserList=[];
        var allEnduserList=this.allEnduserList;
        for(var i=0;i<allEnduserList.length;i++){
          if(allEnduserList[i].enduserOpenId!=enduserOpenId){
            newAllEnduserList.push(allEnduserList[i]);
          }
        }
        console.log(newAllEnduserList);
        this.newAllEnduserList=newAllEnduserList;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.enduserId != null) {
            updateEnduser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEnduser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const enduserIds = row.enduserId || this.ids;
      this.$modal.confirm('是否确认删除终端经营用户编号为"' + enduserIds + '"的数据项？').then(function() {
        return delEnduser(enduserIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('washer/enduser/export', {
        ...this.queryParams
      }, `enduser_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
